
$(function() {
    validateKickout();
    validateRule();
    $('.imgcode').click(function() {
        var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
        $(".imgcode").attr("src", url);
    });
    
    $('.sendTelCode').click(()=>{
    	sendTelCode()
    });
});

$.validator.setDefaults({
    submitHandler: function() {
        login();
    }
});

function login() {
    $.modal.loading($("#btnSubmit").data("loading"));
    var username = $.common.trim($("input[name='username']").val());
    var password = $.common.trim($("input[name='password']").val());
    var validateCode = $("input[name='validateCode']").val();
    var rememberMe = $("input[name='rememberme']").is(':checked');
    //手机验证码
    var telCode = $("input[name='telCode']").val();
    $.ajax({
        type: "post",
        url: ctx + "login",
        data: {
            "username": username,
            "password": password,
            "validateCode": validateCode,
            "telCode": telCode,
            "rememberMe": rememberMe
        },
        success: function(r) {
            if (r.code == web_status.SUCCESS) {
                location.href = ctx + 'index';
            } else {
            	$('.imgcode').click();
            	$(".code").val("");
            	$.modal.msg(r.msg);
            }
            $.modal.closeLoading();
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            },
            telCode: {
                required: true
            }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名",
            },
            password: {
                required: icon + "请输入您的密码",
            },
            telCode: {
                required: icon + "请输入手机验证码",
            }
        }
    })
}

function validateKickout() {
    if (getParam("kickout") == 1) {
        layer.alert("<font color='red'>您已在别处登录，请您修改密码或重新登录</font>", {
            icon: 0,
            title: "系统提示"
        },
        function(index) {
            //关闭弹窗
            layer.close(index);
            if (top != self) {
                top.location = self.location;
            } else {
                var url = location.search;
                if (url) {
                    var oldUrl = window.location.href;
                    var newUrl = oldUrl.substring(0, oldUrl.indexOf('?'));
                    self.location = newUrl;
                }
            }
        });
    }
}

function getParam(paramName) {
    var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}
//发送短信冷却值 默认60秒
var countDownDefault = 60;
//倒计时值
var countDown = 0;
//发送手机验证码
function sendTelCode(){
	let username = $.common.trim($("input[name='username']").val());
	if(username.length == 0){
		layer.alert("<font color='red'>请先填写用户名</font>", {
            icon: 0,
            title: "系统提示"
        })
		return false;
	}
	if(countDown > 0){
		return false;
	}
	
	$.modal.loading("请稍后");
	setTimeout(()=>{//loading最长5秒
		$.modal.closeLoading();
	},5000)
	
	 //请求短信接口
	 $.ajax({
        type: "post",
        url: ctx + "captcha/sendSms",
        data: {
            "username": username
        },
        success: function(r) {
            if (r.code == web_status.SUCCESS) {
                //请求成功
                cooldown()
            }else {
            	$.modal.msg(r.msg);
            }
            $.modal.closeLoading();
        }
	  });
	
}
//发送后倒计时
function cooldown(){
	console.log(countDown)
	if(countDown == 0){
		countDown = countDownDefault
	}
	if(countDown == 1){
		$(".sendTelCode").text("发送验证码");
		countDown = 0;
	}else{
		countDown = countDown -1;
		$(".sendTelCode").text(countDown+"s后重试");
		setTimeout(()=>{
			cooldown()
		},1000);
	}
	
}